from django.db import models
from django.contrib.auth.models import User, Group
from zenith.variables import *

# Create your models.here.
class UserProfile( models.Model ):
	"""
	Profile of a user,
	You can add many other fields
	"""
	user = models.ForeignKey( User, unique = True )

	gender = models.CharField( max_length = 1, choices = GENDER_CHOICES )
	birthday = models.DateField()

class Record( models.Model ):
	"""
	A record,
	recursive relationship is used for hierarchical groups.
	"""
	def __unicode__( self ):
		return self.name.decode( 'utf-8' )

	parent = models.ForeignKey( 'self', null = 'True', blank = 'True' )
	
	rectype = models.CharField( max_length = 1, choices = TYPE_CHOICES )
	abbr = models.CharField( max_length = 128, unique = True )
	name = models.CharField( max_length = 60 )
	start_date = models.DateField()
	description = models.TextField()

	"""
	Permissions to read and write are granted respectively for owner_user, owner_group and other users.
	"""
	owner_user = models.ForeignKey( User )
	owner_groups = models.ManyToManyField( Group, blank = True )
	ouser_write = models.BooleanField( default = True )
	ouser_read = models.BooleanField( default = True )
	ogroup_write = models.BooleanField( default = False )
	ogroup_read = models.BooleanField( default = True )
	others_write = models.BooleanField( default = False )
	others_read = models.BooleanField( default = False )

class FileRecord( models.Model ):
	record = models.OneToOneField( Record )
	diaplay_name = models.CharField( max_length = 256 )
	file_name = models.CharField( max_length = 32, unique = True )
	down_times = models.IntegerField( default = 0 )
	mimetype = models.CharField( max_length = 160 )

class Comment( models.Model ):
	record = models.ForeignKey( Record )
	user = models.ForeignKey( User )
	pub_datetime = models.DateTimeField( auto_now_add = True )
	text = models.TextField()

class UserToAccept( models.Model ):
	user = models.ForeignKey( User )
	group = models.ForeignKey( Group )
	req_date = models.DateField( auto_now = True )

class DownKey( models.Model ):
	record = models.ForeignKey( FileRecord )
	user = models.ForeignKey( User )
	key = models.CharField( max_length = 32, unique = True )
	date = models.DateField()
	unique_together = ( 'record', 'user' )
